﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using PMC;
using PMC.Plugins;
using PMC.Scripts;
using PMC.Structs;

namespace PMCgui
{
    public partial class WebsiteForm : Form
    {
        private List<Script> _scripts;
        private Website current;
        private bool edit = true;

        public WebsiteForm(Website current)
        {
            InitializeComponent();
            this.current = current;
            _scripts = Plugin<Script>.Plugins.Instances().ToList();
            StartPosition = FormStartPosition.CenterParent;

            //foreach (var plugin in _scripts)
            //{
            //    cbScript.Items.Add(plugin.GetPluginInfoByCulture("en").Title); //TODO Make this invariant
            //}
            cbScript.DataSource = _scripts;

            if (_scripts.Count > 0)
                cbScript.SelectedIndex = 0;

            if (current == null)
                return;

            tbName.Text = current.Name;
            tbWebpage.Text = Convert.ToString(current.Webpage);
            tbUsername.Text = current.Username;
            tbPassword.Text = current.Password;
            cbScript.SelectedItem = current.Script;
        }

        public WebsiteForm()
            :this(null)
        {
            edit = false;
            btCancel.Text = Language.Instance.Cancel;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            Uri uri = null;
            if (string.IsNullOrWhiteSpace(tbName.Text) || string.IsNullOrWhiteSpace(tbWebpage.Text) || string.IsNullOrWhiteSpace(tbUsername.Text) || string.IsNullOrWhiteSpace(tbPassword.Text) || !Uri.TryCreate(tbWebpage.Text, UriKind.Absolute, out uri))
            {
                MessageBox.Show(Language.Instance.FieldWhiteSpacedOrFormat);
                DialogResult = DialogResult.None;

            }
            else
            {
                if (current == null)
                {
                    try
                    {
                        Website.Add(tbName.Text, uri, tbUsername.Text, tbPassword.Text, (Script) cbScript.SelectedItem);
                    }
                    catch(DuplicateNameException<Website>)
                    {
                        MessageBox.Show(string.Format(Language.Instance.FormatDuplicateName, tbName.Text));
                        DialogResult = DialogResult.None;
                    }
                }
                else
                {
                    current.Name = tbName.Text;
                    current.Webpage = uri;
                    current.Username = tbUsername.Text;
                    current.Password = tbPassword.Text;
                    current.Script = (Script) cbScript.SelectedItem;
                }
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            if (current != null)
                current.Delete();
        }
    }
}
